/*
 *  Copyright 2012 yura.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package org.opu.pc.services;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.opu.pc.domain.PublicationManagement;
import org.opu.pc.domain.domains.Publication;
import org.opu.pc.domain.domains.Publisher;
import org.opu.pc.publications_analysis.PublicationAnalyticService;
import org.opu.pc.services.beans.PublicationBean;
import org.opu.pc.services.beans.PublisherBean;

/**
 *
 * @author yura
 */
public class PublicationServiceImpl implements PublicationService {

    private PublicationManagement publicationManagement;
    private PublicationAnalyticService publicationAnalyticService;


    
    public PublicationServiceImpl(PublicationManagement publicationManagement,
			PublicationAnalyticService publicationAnalyticService) {
		this.publicationManagement = publicationManagement;
		this.publicationAnalyticService = publicationAnalyticService;
	}

	@Override
    public List<PublicationBean> list() {
        return toBeans(publicationManagement.list());
    }

    @Override
    public List<PublicationBean> searchByName(String name, Boolean wasApplied) {
        return toBeans(publicationManagement.searchByName(name, wasApplied));
    }

    @Override
    public List<PublicationBean> publicationsForPublisher(
            PublisherBean publicator, Boolean wasApplied) {

        return toBeans(publicationManagement.publicationsForPublisher(
                publicator.getId(), wasApplied));
    }

    @Override
    public PublicationBean createNew(PublicationBean publicationBean,
            List<PublisherBean> authors) {

        Publication publication = new Publication();
        copyData(publication, publicationBean);

        publication = publicationManagement.createNew(publication, toDomains(authors));

        return toBean(publication);
    }

    @Override
    public void deletePublication(Integer id){
        publicationManagement.deletePublication(id);
    }

    @Override
    public void updatePublication(PublicationBean publicationBean){
        Publication publication = new Publication();
        publication.setId(publicationBean.getId());
        copyData(publication, publicationBean);
        publicationManagement.updatePublication(publication);
    }
    
    @Override
    public void analysFile(PublicationBean publicn, List<PublisherBean> publis, 
    		File file, StringBuffer buffer, int lineNum) throws IOException {
    	
    	Publication publication = publicationAnalyticService.analyzePublication(
    			FileUtils.openInputStream(file), 
    			FilenameUtils.getExtension(file.getName()), buffer, lineNum);
    	
    	publicn.setName(publication.getName());
        publicn.setPublicationPlace(publication.getPublicationPlace());
        publicn.setDate(publication.getDate());
        publicn.setConferenceVenue(publication.getConferenceVenue());
        publicn.setPageNumberStart(publication.getPageNumberStart());
        publicn.setPageNumberStart(publication.getPageNumberEnd());
        publicn.setWasApplied(publication.isWasApplied());
        
        publis.clear();
        
        for (Publisher p : publication.getPublishers()) {
			publis.add(new PublisherBean(-1, p.getName(), 
					p.getSecondName(), p.getLastName(), p.getIsStudent()));
		}
    }

    @Override
    public List<String> getDocumentExtensions() {
    	return publicationAnalyticService.getDocumentExtensions();
    }

    private void copyData(Publication publication, PublicationBean publicationBean) {
        publication.setName(publicationBean.getName());
        publication.setPublicationPlace(publicationBean.getPublicationPlace());
        publication.setDate(publicationBean.getDate());
        publication.setConferenceVenue(publicationBean.getConferenceVenue());
        publication.setPageNumberStart(publicationBean.getPageNumberStart());
        publication.setPageNumberStart(publicationBean.getPageNumberEnd());
        publication.setWasApplied(publicationBean.isWasApplied());
    }

    private PublicationBean toBean(Publication p) {
        return new PublicationBean(p.getId(), p.getName(), 
                p.getPublicationPlace(), p.getDate(), p.getConferenceVenue(),
                p.getPageNumberStart(), p.getPageNumberEnd(), p.wasApplied());
    }

    private List<PublicationBean> toBeans(List<Publication> list) {
        List<PublicationBean> ret = new ArrayList<PublicationBean>(list.size());
        for (Publication p : list) {
            ret.add(toBean(p));
        }
        return ret;
    }

    private List<Publisher> toDomains(List<PublisherBean> beans){
        List<Publisher> list = new ArrayList<Publisher>(beans.size());

        for (PublisherBean p : beans) {
			Publisher publisher = new Publisher(p.getId(), p.getName(),
					p.getSurname(), p.getMiddleName());
			publisher.setIsStudent(p.isStudent());
			list.add(publisher);
        }

        return list;
    }
}
